<?php
namespace app\houtai\Controller;
use app\houtai\controller\Base;
use app\houtai\controller\Tree;
use think\Controller;
use think\Db;
use think\Request;
class Cate extends Base
{
    public function lst()
    {
        $data=db('cate')->select();
        $res = Tree::toList($data); 
        $this->assign('cate',$res);
        return view();
    }
    // 列表
    public function sorts()
    {
        $data= $_POST;
        $cate = new \app\houtai\model\Cate();//model模型成
        $res=$cate->saveAll($data['array']);
        if($res){
            return json_encode([
                'status'=>1,
                ]);
        }else{
            return json_encode([
                'status'=>0,
                ]);
        }
    }
    // 排序
    public function del()
    {
        $id=input('id');
        $datas=db('cate')->where('id',$id)->find();
        $shilds=explode(',',$datas['shilds']);
        if($datas['shild']){
            db('cate')->delete($shilds);
        }
        $res=db('cate')->delete($id);
        if($res){
            return json_encode([
                'status'=>1,
                ]);
        }else{
            return json_encode([
                'status'=>0,
                ]);
        }
    }
    // 单个删除
    public function delall()
    {
        $id=input('id');
        $datas=db('cate')->where('id',$id)->find();
        $shilds=explode(',',$datas['shilds']);
        if($datas['shild']){
            db('cate')->delete($shilds);
        }
        $res=db('cate')->delete($id);
        if($res){
            return json_encode([
                'status'=>1,
                ]);
        }else{
             return json_encode([
                'status'=>0,
                ]);
        }
    }
    // 全删
    public function add()
    {
        $data=db('cate')->select();
        $res = Tree::toList($data); 
        $this->assign('cate',$res);
        return view();
    }
    public function addajax()
    {
        $datas=input('post.');
        $res=Db::name('cate')->insert($datas);
        $userId = Db::name('cate')->getLastInsID();
        switch ($datas['sf']) {
            case 1:
                $datas['url']= DOM.'index.php/index/pages/news/cateid/'.$userId;
                break;
            case 2:
                $datas['url']= DOM.'index.php/index/pages/thumb/cateid/'.$userId;
                break;
            default:
                $datas['url']= DOM.'index.php/index/pages/spage/cateid/'.$userId;
                break;
        }
        db('cate')->where('id',$userId)->update($datas);
        if ($datas['sf']==3) {
            db('page')->data(['cateid'=>$userId])->insert();            
        }
        $shilds=db('cate')->where('id',$datas['pid'])->find();
        if($shilds['shild']){
            $a1=[
            'shilds'=>$shilds['shilds'].','.$userId,
            ];
        }else{
            $a1=[
            'shild'=>1,
            'shilds'=>$shilds['shilds'].','.$userId,
            ];
        }
        db('cate')->where('id',$datas['pid'])->update($a1);
        $ress=Db::name('cate')->where('id',$userId)->setField('sort', $userId);

        if($ress){
            return json_encode([
                'status' =>1,
            ]);
        }else{
            return json_encode([
                'status' =>0,
            ]);
        }
    }
    // 数据添加
    public function edit()
    {   
        $id=input('id');
        if($id!=0){
            // $id=input('id');
            $data=db('cate')->where('id',$id)->find();
            $datas=db('cate')->select();
            $this->assign('edit',$data);
            $res = Tree::toList($datas); 
            $this->assign('cate',$res);
            return view();
        }
    }
    public function editajax()
    {
        $data=input('post.');
        switch ($data['sf']) {
            case 1:
                $data['url']= DOM.'index.php/index/pages/news/cateid/'.$data['id'];
                break;
            case 2:
                $data['url']= DOM.'index.php/index/pages/thumb/cateid/'.$data['id'];
                break;
            default:
                $data['url']= DOM.'index.php/index/pages/spage/cateid/'.$data['id'];
                break;
        }
        $res=db('cate')->update($data);

        if($res){
            return json_encode([
                'status'=>1,
                ]);
        }else{
            return json_encode([
                'status'=>0,
                ]);
        }        
    }
    // 数据修改
    public function emptied()
    {
        $id=input('id');
        if($id){
            $res=Db::table('ch_cate')->execute('TRUNCATE table ch_cate');
            if (!$res) {
                return json_encode([
                    'status'=>1,
                    ]);
            }else{
                return json_encode([
                    'status'=>0,
                    ]);
            }
        }
    }
    // 清空当前数据表内容
}
